package com.lej.my.set

import coil.load
import com.alibaba.android.arouter.facade.annotation.Route
import com.lej.core.base.BaseActivity
import com.lej.core.common.MyToSet
import com.lej.core.common.PCacheInfo
import com.lej.core.common.ToMyMsgLogout
import com.lej.core.router.CRouter
import com.lej.core.utils.extention.resString
import com.lej.core.utils.extention.router
import com.lej.core.utils.extention.toast
import com.lej.dataservice.club.bean.main.my.MyUserInfoAll
import com.lej.my.BR
import com.lej.my.R
import com.lej.my.databinding.MyActivitySetBinding
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.util.QMUIStatusBarHelper

/**
 *  author: cjq
 *  date  : 2022/4/20 0020
 *  desc  : 我的设置页
 */

@Route(path = CRouter.mySet)
class MySetActivity : BaseActivity<MyActivitySetBinding, MySetViewModel>() {
    override fun getLayout(): Int = R.layout.my_activity_set

    override fun initVariableId(): Int = BR.vm

    override fun initData() {
        super.initData()
        QMUIStatusBarHelper.translucent(this)
        QMUIStatusBarHelper.setStatusBarDarkMode(this)
        val muUserInfo: MyUserInfoAll? = intent.getParcelableExtra(MyToSet)
        muUserInfo?.let { viewModel.userinfo.value = it }
        PCacheInfo.nick.takeIf { it.length >= 11 }?.let {
            viewModel.phone.value = it.replaceRange(3, 9, "*****")
        }
        initClick()
    }

    private fun initClick() {
        binding.clHead.onClick {
            R.string.my_set_update_tips.resString.toast()
        }
        binding.clNick.onClick {
            R.string.my_set_update_tips.resString.toast()
        }
        binding.clTel.onClick {
            R.string.my_set_update_tips.resString.toast()
        }
        binding.title.iconLeft?.onClick {
            finish()
        }
        binding.clMsg.onClick {
            router(CRouter.myMsgLogout) {
                withBoolean(ToMyMsgLogout, true)
            }
        }

        binding.clLogout.onClick {
            router(CRouter.myMsgLogout) {
                withBoolean(ToMyMsgLogout, false)
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.userinfo.observe(this) {
            binding.ivHead.load(it.avatar) {
                placeholder(R.drawable.ic_default_icon)
                error(R.drawable.ic_default_icon)
            }
        }
    }
}